﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Utility
{
    public class InviteMessage : BaseMessage
    {
        public InviteMessage()
        {
            Type = InviteMessageType.OK;
            Target = "";
        }

        public InviteMessage(byte[] bytes)
        {
            int offset = 0;
            Type = (InviteMessageType) BitConverter.ToInt32(bytes, offset);
            offset += 4;
            int nameSize = BitConverter.ToInt32(bytes, offset);
            offset += 4;
            Target = Encoding.UTF8.GetString(bytes, offset, nameSize);
        }

        public InviteMessage(string msg)
            : this(Encoding.UTF8.GetBytes(msg))
        {
        }

        public InviteMessageType Type { get; set; }
        public string Target { get; set; }

        public override byte[] ToByteArray()
        {
            var ls = new List<byte>();
            ls.AddRange(BitConverter.GetBytes((int) Type));
            ls.AddRange(BitConverter.GetBytes(Target.Length));
            ls.AddRange(Encoding.UTF8.GetBytes(Target));
            return ls.ToArray();
        }

        public override string ToString()
        {
            return Encoding.UTF8.GetString(ToByteArray());
        }
    }
}